﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DSPL.DataAccess;
using Microsoft.Practices.Unity;
using System.Diagnostics;
using System.Windows.Forms;
using DSPL.Contracts;
using DSPL.Contracts.Interfaces;
using DSPL.Services.Help;

namespace DSPL.Services.Services
{
    public class ReadService : IReadService
    {
        DatabaseAccess db;

        #region IReadService Members

        public DataTable GetAllMasterData(string tablename)
        {
            db  =DatabaseAccess.Instance ;
            Query query = new Query() { TableName = tablename, QType = QueryType.SELECTALL };
            return db.ExcecuteSelect(query);
        }

        public DataTable GetAllMasterDataTopRecords(string tablename, int numberOfRecPerPage)
        {
            db  =DatabaseAccess.Instance ;
            Query query = new Query() { TableName = tablename, QType = QueryType.SELECTTOPNALL, NumberOfRecords= numberOfRecPerPage };
            return db.ExcecuteSelect(query);
        }

        public DataTable GetDummyRecord(string tablename)
        {
            db  =DatabaseAccess.Instance ;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();
            whereList.Add(new ColumnWithValue() { ColumnName = "1", ColumnValue = "0" });
            Query query = new Query() { TableName = tablename, QType = QueryType.SELECTALL, WhereClause = whereList.ToArray() };
            
            return db.ExcecuteSelect(query);
        }
        
        public DataTable GetDuplicates(string tablename, string searchkey, string searchkeyValue)
        {
            db  =DatabaseAccess.Instance ;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();
            whereList.Add(new ColumnWithValue() { ColumnName = searchkey, ColumnValue = searchkeyValue });
            Query query = new Query() { TableName = tablename, QType = QueryType.SELECTALL, WhereClause = whereList.ToArray() };

            return db.ExcecuteSelect(query);
        }

        public DataTable GetDuplicates(string tableName, IColumnWithValue[] uniqueKeyList)
        {
            db  =DatabaseAccess.Instance ;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();

            foreach (var item in uniqueKeyList)
            {
                whereList.Add(new ColumnWithValue() { ColumnName = item.ColumnName, ColumnValue = item.ColumnValue, ColumnDataType = item.ColumnDataType, ColumnConnector = QLogicOperator.AND });
            }
            //ColumnWithValue[] whereList = UniqueKeyList as ColumnWithValue[];
            Query query = new Query() { TableName = tableName, QType = QueryType.SELECTALL, WhereClause = whereList.ToArray() };
            
            return  db.ExcecuteSelect(query);
        }

        public object CheckValidityOfDeletion(string tableName, int primaryKey,string primaryKeyField, string ingnoreTableList = "")
        {
            db  =DatabaseAccess.Instance ;
            List<ParameterWithValue> paramList = new List<ParameterWithValue>();
            paramList.Add(new ParameterWithValue("@tableName", tableName));
            paramList.Add(new ParameterWithValue("@ingnoreTableList", ingnoreTableList));
            paramList.Add(new ParameterWithValue("@pk", primaryKey));
            paramList.Add(new ParameterWithValue("@fieldName", primaryKeyField));
            paramList.Add(new ParameterWithValue("@result", true, SqlDbType.Bit, ParameterDirection.InputOutput));

            SpCaller spCaller = new SpCaller() { SpName = "SP_CHECK_DELETION_VALIDITY", SpParamValues = paramList.ToArray() };
            return (object)db.Excecute(spCaller, "@result");
        }

        public object CheckValidityAccountCodesDeletion(string tableName, string primaryKeyField,DataTable dtData, string ingnoreTableList = "")
        {
            db = DatabaseAccess.Instance;
            List<ParameterWithValue> paramList = new List<ParameterWithValue>();
            paramList.Add(new ParameterWithValue("@tableName", tableName));
            paramList.Add(new ParameterWithValue("@ignoreTableList", ingnoreTableList));            
            paramList.Add(new ParameterWithValue("@fieldName", primaryKeyField));
            paramList.Add(new ParameterWithValue("@accTable", dtData,SqlDbType.Structured));
            paramList.Add(new ParameterWithValue("@result", true, SqlDbType.Bit, ParameterDirection.InputOutput));

            SpCaller spCaller = new SpCaller() { SpName = "SP_CHECK_DELETION_VALIDITY_ACC_CODES", SpParamValues = paramList.ToArray() };
            return (object)db.Excecute(spCaller, "@result");
        }

        public int GetPrimaryKeyValue(string tableName,string keyFieldName,string keyFieldValue,string primaryKeyFieldName)
        {
            int keyValue=0;
            db  =DatabaseAccess.Instance ;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();

            whereList.Add(new ColumnWithValue() { ColumnName =keyFieldName, ColumnValue = keyFieldValue, Comparison=QComparison.EQUALS  });
                      
            Query query = new Query() { TableName = tableName, QType = QueryType.SELECTALL, WhereClause = whereList.ToArray() };

            var dataTable= db.ExcecuteSelect(query);
            if (dataTable.Rows.Count == 1)
            {
                keyValue = Convert.ToInt32(dataTable.Rows[0][primaryKeyFieldName]);
            }
           return keyValue;
        }

        public string GetUniqueCodeValue(string tableName, string primaryKeyFieldName, string keyFieldValue, string KeyFieldName)
        {
            string keyCodeValue = string.Empty;
            db =  DatabaseAccess.Instance;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();

            whereList.Add(new ColumnWithValue() { ColumnName = primaryKeyFieldName, ColumnValue = keyFieldValue, Comparison = QComparison.EQUALS });

            Query query = new Query() { TableName = tableName, QType = QueryType.SELECTALL, WhereClause = whereList.ToArray() };

            var dataTable = db.ExcecuteSelect(query);
            if (dataTable.Rows.Count == 1)
            {
                keyCodeValue = Convert.ToString(dataTable.Rows[0][KeyFieldName]);
            }
            return keyCodeValue;
        }

        public DataTable GetAllMasterDataByHelpName(HelpNames helpName)
        {
            db = DatabaseAccess.Instance;
            Query query = new HelpBuilder().GetHelpBuilder(helpName);
            return db.ExcecuteSelect(query);
        }

        public DataTable GetAllTransactionData(string tableName, string primaryKeyFieldName)
        {
            db = DatabaseAccess.Instance;
            List<ColumnWithValue> columnList = new List<ColumnWithValue>();
            columnList.Add(new ColumnWithValue { ColumnName = primaryKeyFieldName });
            Query query = new Query() { TableName = tableName, ColValues = columnList.ToArray(), QType = QueryType.SELECT };
            return db.ExcecuteSelect(query);
        }


        public DataTable GetChildTransactionData(string childTableName, string primaryKeyField, object primaryKeyValue)
        {
            db = DatabaseAccess.Instance;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();
            whereList.Add(new ColumnWithValue { ColumnName = primaryKeyField, ColumnValue = primaryKeyValue, Comparison = QComparison.EQUALS });
            Query query = new Query() { TableName = childTableName, WhereClause = whereList.ToArray(), QType = QueryType.SELECTALL };
            return db.ExcecuteSelect(query, true);
        }

        public DataTable GetChildTransactionData(HelpNames childGridHelpName, object primaryKeyValue)
        {
            db = DatabaseAccess.Instance;
            Query query = new HelpBuilder().GetHelpBuilder(childGridHelpName, primaryKeyValue);
            return db.ExcecuteSelect(query, true);
        }

        public DataTable GetParentTransactionData(string parentTableName, string primaryKeyField, object primaryKeyValue)
        {
            db = DatabaseAccess.Instance;
            List<ColumnWithValue> whereList = new List<ColumnWithValue>();
            whereList.Add(new ColumnWithValue { ColumnName = primaryKeyField, ColumnValue = primaryKeyValue, Comparison = QComparison.EQUALS });
            Query query = new Query() { TableName = parentTableName, WhereClause = whereList.ToArray(), QType = QueryType.SELECTALL };
            return db.ExcecuteSelect(query, true);
        }

        public DataRow GetPreviousOrNextTransactionRecord(string tableName, int primaryKey, string primaryKeyField, bool isLast)
        {
            db = DatabaseAccess.Instance;
            List<ParameterWithValue> paramList = new List<ParameterWithValue>();
            paramList.Add(new ParameterWithValue("@tableName", tableName));
            paramList.Add(new ParameterWithValue("@primaryKey", primaryKey));
            paramList.Add(new ParameterWithValue("@isLast", isLast));
            paramList.Add(new ParameterWithValue("@primaryKeyFieldName", primaryKeyField));

            SpCaller spCaller = new SpCaller() { SpName = "SP_GET_NEXT_PREV_RECORD", SpParamValues = paramList.ToArray() };
            DataTable dt = db.ExcecuteSelect(spCaller);
            if (dt == null || dt.Rows.Count == 0)
                return null;
            return dt.Rows[0];
        }

        #endregion


        
    }
}
